package com.icbc.dataDisplayService.controller;

import com.alibaba.dubbo.container.page.PageHandler;
import com.github.pagehelper.PageHelper;
import com.icbc.dataDisplayService.bean.Car;
import com.icbc.dataDisplayService.bean.GPS;
import com.icbc.dataDisplayService.service.CarDataService;
import com.icbc.dataDisplayService.service.GPSdataService;
import net.sf.json.JSONArray;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CarDataController {

    @RequestMapping(value = "/")        //网页启动控制器
    public ModelAndView carDataHandler(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception{

        //实例化服务bean
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:xml/mybatis/applicationContext-MFBean.xml");
        CarDataService carDataService = (CarDataService) ac.getBean("carDataService");

        //启用mybatis-hpagehelper,获取注入分页信息到sql语句
//        PageHelper.startPage(1,10);
//        List<Car> DataList = carDataService.selectAllCar();

        //返回数据模型和视图信息
        ModelAndView mav = new ModelAndView();
//        Map<String,List> weatherToJSP = new HashMap<String, List>();
//        weatherToJSP.put("now",DataList);
//        mav.addObject("map",weatherToJSP);
        mav.setViewName("gps");

        return mav;
    }

    @RequestMapping(value="/ServicePagination",method = RequestMethod.GET)      //table分页、自定义搜索框控制器
    @ResponseBody //此注解不能省略 否则ajax无法接受返回值
    public void carPagination(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {

        //设定编码
        httpServletResponse.setCharacterEncoding("UTF-8");
        //表示是json类型的数据
        httpServletResponse.setContentType("application/json");  //解决：XML 解析错误：格式不佳

        //得到客户端传递的页码和每页记录数，并转换成int类型
        int pageNumber = Integer.parseInt(httpServletRequest.getParameter("pageNumber"));
        int pageSize = Integer.parseInt(httpServletRequest.getParameter("pageSize"));
        int search = Integer.parseInt(httpServletRequest.getParameter("search"));

        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:xml/mybatis/applicationContext-MFBean.xml");
        CarDataService carDataService = (CarDataService) ac.getBean("carDataService");

        //自定义搜索框功能后台代码
        if (search == 0) {      //分页
            PageHelper.startPage(pageNumber, pageSize);
            List<Car> DataList = carDataService.selectAllCar();

            int total = carDataService.countAllCar();

            String listToJson = JSONArray.fromObject(DataList).toString();
            String json = "{\"total\":" + total + ",\"rows\":" + listToJson + "}";

            httpServletResponse.getWriter().println(json);      //这样来写也是可以的
        }
        else {          //搜索框数据返回
            int single = 1;
            Car carDataById = carDataService.getCarDataById(search);
            String singleToJson = JSONArray.fromObject(carDataById).toString();
            String singleJson = "{\"total\":" + single + ",\"rows\":" + singleToJson + "}";

            httpServletResponse.getWriter().println(singleJson);        //返回json格式数据到页面
        }

//        return json;        //为什么这里一定要有返回值？不然会出现<XML 解析错误：找不到根元素>的错误
    }

    @RequestMapping(value = "/DClickEvent")     //表格行双击事件控制器，后台可以根据此处carid获取汽车的定位和轨迹数据，返回给页面显示
    @ResponseBody
    public void DClickEvent(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        //设定编码
        httpServletResponse.setCharacterEncoding("UTF-8");
        //表示是json类型的数据
        httpServletResponse.setContentType("application/json");  //解决：XML 解析错误：格式不佳

        int carid = Integer.parseInt(httpServletRequest.getParameter("carid"));

        String json = "{\"carid\":" + carid + "}";

        httpServletResponse.getWriter().println(json);
    }
}
